package services

import (
	"github.com/gin-gonic/gin"
	. "yunwei/go_api/application/model"
)

/*
添加指令模板类别
*/
func CreateCmdCategory(ctx *gin.Context) (CommandCategoryInstance, error) {
	hostCategory := CommandCategory{}
	var instance CommandCategoryInstance
	var err error
	if err = ctx.ShouldBindJSON(&hostCategory); err != nil {
		return instance, err
	}
	err = hostCategory.Insert()
	instance = CommandCategoryInstance{ID: hostCategory.ID, Name: hostCategory.Name}
	return instance, err
}

/*
获取指令模板类别列表
*/
func GetCmdCategoryList(ctx *gin.Context) ([]CommandCategoryInstance, error) {
	hostCategory := CommandCategory{}
	hostCategoryList, err := hostCategory.GetAll()
	return hostCategoryList, err
}

/*
*
添加指令模板
*/
func CreateCmd(ctx *gin.Context) (CommandInstance, error) {
	cmd := Command{}
	var err error
	var instance CommandInstance
	var cmdCategory CommandCategory

	if err = ctx.ShouldBindJSON(&cmd); err != nil {
		return instance, err
	}

	err = cmd.Insert()
	if err != nil {
		return instance, err
	}

	err = cmdCategory.GetOneById(cmd.CommandCategoryID)
	if err != nil {
		return instance, err
	}

	instance = CommandInstance{
		ID:           cmd.ID,
		Name:         cmd.Name,
		Content:      cmd.Content,
		CategoryID:   cmdCategory.ID,
		CategoryName: cmdCategory.Name,
	}
	return instance, err
}

/**
获取指令模板列表
*/

func GetCmdList(name string, cmdCategoryId uint) ([]CommandInstance, error) {
	var cmdList []Command
	var instanceList []CommandInstance
	var cmd Command
	var err error
	cmdList, err = cmd.GetAll(name, cmdCategoryId)
	for _, cmdItem := range cmdList {
		instanceList = append(instanceList, CommandInstance{
			ID:           cmdItem.ID,
			Name:         cmdItem.Name,
			Content:      cmdItem.Content,
			CategoryID:   cmdItem.CommandCategory.ID,
			CategoryName: cmdItem.CommandCategory.Name,
		})
	}
	return instanceList, err
}

/**
获取指令模板信息
*/

func GetCmd(id int) (CommandInstance, error) {
	cmd := Command{}
	err := cmd.GetOneById(uint(id))
	instance := CommandInstance{
		ID:           uint(id),
		Name:         cmd.Name,
		Content:      cmd.Content,
		CategoryID:   cmd.CommandCategory.ID,
		CategoryName: cmd.CommandCategory.Name,
	}
	return instance, err
}
